<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\Admin;
use app\admin\controller\api\Library as LibraryApi;
use think\config\driver\Json;

/**
 * 字典管理
 * @package app\admin
 */
class Library  extends Admin

{
	protected $api;
	
	public function _initialize(){
		parent::_initialize();
		$this->api = new LibraryApi();
	}
	
	/**
	 * 字典列表
	 * $return View/Json
	 */
	public function index(){
		if(IS_AJAX){
			return $this->api->libraryList();
		}
		$libraryItemList = $this->api->libraryItemList()->getData();
		$this->assign('libraryItem',$libraryItemList['data']);
		return $this->fetch();
	}
	
	/**
	 * 字典查询
	 * @return Json
	 */
	public function search(){
		if(IS_AJAX){
			return $this->api->search(input('sysCode'));
		}
	}
	
	/**
	 * 字典新增
	 * @return View/Json
	 */
	public function create() {
		if(IS_POST){
			$data = input('post.');
			$lastSysCode = model('Library')->order('sysCode','desc')->limit(1)->value('sysCode');
			$data['sysCode'] = $lastSysCode+1;
			$data['subsystemNo'] = 1;
			$data['orderNumber'] = 1;
			return $this->api->add($data);
		}
		$libraryItemList = $this->api->libraryItemList()->getData();
		$this->assign('libraryItem',$libraryItemList['data']);
		return $this->fetch();
	}
	
	/**
	 * 字典添加
	 * @return View/Json
	 */
	public function add() {
		if(IS_POST){
			$data = input('post.');
			$lastSubsystemNo = model('Library')->where('sysCode',$data['sysCode'])->order('subsystemNo','desc')->limit(1)->value('subsystemNo');
			$lastOrderNumber = model('Library')->where('sysCode',$data['sysCode'])->order('orderNumber','desc')->limit(1)->value('orderNumber');
			$data['subsystemNo'] = $lastSubsystemNo+1;
			$data['orderNumber'] = $lastOrderNumber+1;
			return $this->api->add($data);
		}
		$libraryItemList = $this->api->libraryItemList()->getData();
		$this->assign('libraryItem',$libraryItemList['data']);
		return $this->fetch();
	}
	
	/**
	 * 字典编辑
	 * @param string $libraryId
	 * @return \think\response\Json|mixed
	 */
	public function edit($libraryId){
		if(IS_POST){
			$data = input('post.');
			return $this->api->edit($data,$libraryId);
		}
		$library = model('Library')->find($libraryId);
		$libraryItemList = $this->api->libraryItemList()->getData();
		$this->assign('libraryItem',$libraryItemList['data']);
		$librarySubstystemList = model('Library')->where('sysCode',$library['parentCode'])->select();
		$this->assign('librarySubsystem',$librarySubstystemList);
		$this->assign('info',$library);
		return $this->fetch();
	}
	
	/**
	 * 字典删除
	 */
	public function del(){
		if(IS_GET){
			return $this->api->del(input('libraryId'));
		}
	}
	
	
}